﻿#include <iostream>
#include <boost/asio.hpp>

using namespace boost::asio::ip;
using namespace std;

const int MAX_LENTH = 1024;

int main()
{
	try
	{
		// 创建上下文服务
		boost::asio::io_context ioc;
		// 构造endpoint
		tcp::endpoint remote_ep(address::from_string("127.0.0.1"), 10086);
		// 创建Socket
		tcp::socket sock(ioc);
		boost::system::error_code error = boost::asio::error::host_not_found;
		sock.connect(remote_ep, error);
		if (error)
		{
			cout << "connect fail, code is " << error.value() << " Message: " << error.what() << endl;
			return 0;
		}
		cout << "connect success" << endl;
		cout << "Enter message: ";
		char request[MAX_LENTH];
		cin.getline(request, MAX_LENTH);
		size_t request_length = strlen(request);
		boost::asio::write(sock, boost::asio::buffer(request, request_length));

		char reply[MAX_LENTH];
		size_t reply_length = boost::asio::read(sock, boost::asio::buffer(reply, request_length));
		cout << "Reply is ";
		cout.write(reply, reply_length);
		cout << endl;

	}
	catch(exception& e)
	{
		cerr << e.what() << endl;
	}
	return 0;
}